import pandas as pd
import numpy as np

import fileUtil
from 每日工作 import run
from 每日工作.InputFile import Input


def invoke():
    data1 = pd.read_excel(Input.pre_warning_1)
    data2 = pd.read_excel(Input.pre_warning_2)

    data = pd.concat([data2, data1], ignore_index=True)

    # 筛选分公司

    data = run.filter_company(data, '分公司')
    # 筛选时间

    data['预警发生时间'] = pd.to_datetime(data['预警发生时间'])

    date = pd.Timestamp(Input.startTime)
    data = data.loc[data['预警发生时间'] >= date]
    date = pd.Timestamp(Input.endTime)
    data = data.loc[data['预警发生时间'] <= date]

    sheet2 = data
    # 筛选指定列
    data = data[
        ['分公司', '预警等级', '预警类型', '预警发生时间', '时间段', '设备名称', '流水号']]

    # 插入三列
    data.insert(6, '是否需调研', np.nan)
    data.insert(6, '跟踪结论', np.nan)
    data.insert(6, '是否跟踪', np.nan)
    # 使用xlsxwriter引擎写入Excel文件并设置列高亮

    current_date = Input.datetime
    path = f"/家宽/中断投诉跟踪表/中断投诉跟踪表{current_date}/3、预警跟踪结论表{current_date}.xlsx"
    fileUtil.mkdir(path)

    with pd.ExcelWriter(path,
                        engine='xlsxwriter') as writer:
        data.to_excel(writer, sheet_name='Sheet1', index=False)
        sheet2.to_excel(writer, sheet_name='明细', index=False)
        workbook = writer.book
        worksheet = writer.sheets['Sheet1']

        # 设置要高亮的列（这里以第一列为例）
        format = workbook.add_format({'bg_color': 'yellow'})
        worksheet.set_column(6, 8, None, format)
        worksheet.set_column(5, 5, 61)
